#include <gtest/gtest.h>
#include <proto_service.hpp>
#include <proto_client.hpp>
#include "base.grpc.pb.h"
#include "base_client.hpp"

constexpr int port = 51120;

TEST(proto_async,meta_data)
{
    asio::io_context io;
    auto& datas = asio::use_service<GrpcMetaDataService>(io);
    datas.get_metadata()->set("hello","world");
    using CType = GRpcServerContext<test_service::AsyncService>;
    auto& context = asio::use_service<CType>(io);
    auto& svc = asio::use_service<test_service_service>(io);
    context.listen(port);
    std::string meta_value = "-1";
    context.bind(&test_service::AsyncService::Requesttest_inc,[&meta_value](meta_data* storage,const base& req)->asio::awaitable<base>{
        meta_value = *storage->client_data.get("hello");
        auto r = req;
        r.set_a(r.a()+1);
        co_return std::move(r);
    });
    int inc_result = -1;
    svc.connect(grpc::CreateChannel(std::string{"localhost:"}+std::to_string(port), grpc::InsecureChannelCredentials()));
    asio::co_spawn(io,[&]()->asio::awaitable<void>{
        base req;
        req.set_a(1);
        inc_result = (co_await svc.test_inc(req)).a();
        io.stop();
        co_return;
    },asio::detached);
    io.run();
    ASSERT_EQ(inc_result,2);
    ASSERT_EQ(meta_value,"world");

}